package com.basemap.api;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.basemap.GZtianditu.GZTianDiTuLayer;
import com.basemap.GZtianditu.GZTianDiTuLayerBuilder;
import com.basemap.GZtianditu.GZTianDiTuLayerTypes;
import com.basemap.tianditu.TianDiTuLayer;
import com.basemap.tianditu.TianDiTuLayerBuilder;
import com.basemap.tianditu.TianDiTuLayerTypes;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.LayerList;

import java.io.File;

public class MapApi {


    public static String gzTdtTK;//贵州天地图秘钥
    public static String gjTdtTK;//国家天地图秘钥
    public String cachePath;//天地图缓存路径
    private GZTianDiTuLayer tdt_gz, tdt_gz2;
    private TianDiTuLayer tdtgz, tdtgz2;


    public MapApi(String myGzTdtTK, String myGjTdtTK, String mCachePath) {
        gjTdtTK = myGjTdtTK;
        gzTdtTK = myGzTdtTK;
        cachePath = mCachePath;
    }

    /**
     * 注意使用前必须获取到存储权限
     *
     * @param context    上下文
     * @param mArcGISMap
     * @param type       1:贵州天地图 2：国家天地图
     * @param year       贵州天地图时需要加载的年份 2018-2022年；为空则默认最新一年
     * @param imgType    1:矢量 2：影像
     */
    protected void loadBaseMap(Context context, ArcGISMap mArcGISMap, int type, String year, int imgType) {
        if(cachePath!=null){
            File file = new File(cachePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        }


        ProgressDialog progressDialog = ProgressDialog.show(context, null, "正在切换地图...", true, true);
        try {
            LayerList baseLayers = mArcGISMap.getBasemap().getBaseLayers();
            baseLayers.clear();
            tdt_gz = null;
            tdt_gz2 = null;
            Basemap basemap = new Basemap();
            if (type == 1) {
                int tiandituImage = GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ2023;

                if (year == null || "".equalsIgnoreCase(year)) {
                    tiandituImage = GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ2022;
                } else if (year.equals("2022")) {
                    tiandituImage = GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ2022;
                } else if (year.equals("2021")) {
                    tiandituImage = GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ_2000;
                } else if (year.equals("2020")) {
                    tiandituImage = GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ2020_2000;
                } else if (year.equals("2019")) {
                    tiandituImage = GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ2019_2000;
                } else if (year.equals("2018")) {
                    tiandituImage = GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ2018_2000;
                }
                tdt_gz = new GZTianDiTuLayerBuilder().setContext(context)
                        .setLayerType(tiandituImage)
                        .setCachePath(cachePath)
                        .setType("GZtianditu")
                        .build();
                tdt_gz2 = new GZTianDiTuLayerBuilder().setContext(context)
                        .setLayerType(GZTianDiTuLayerTypes.TIANDITU_IMAGE_GZ_ANNOTATION_CHINESE_2000)
                        .setCachePath(cachePath)
                        .setType("GZtianditu")
                        .build();
                basemap.getBaseLayers().add(tdt_gz);
                basemap.getBaseLayers().add(tdt_gz2);

            } else {
                //                1:矢量 2：影像
                if (imgType == 1) {
                    tdtgz = new TianDiTuLayerBuilder().setContext(context)
                            .setLayerType(TianDiTuLayerTypes.TIANDITU_VECTOR_2000)
                            .setCachePath(cachePath)
                            .setType("tianditu")
                            .build();
                    tdtgz2 = new TianDiTuLayerBuilder().setContext(context)
                            .setLayerType(TianDiTuLayerTypes.TIANDITU_VECTOR_ANNOTATION_CHINESE_2000)
                            .setCachePath(cachePath)
                            .setType("tianditu")
                            .build();
                } else {
                    tdtgz = new TianDiTuLayerBuilder().setContext(context)
                            .setLayerType(TianDiTuLayerTypes.TIANDITU_IMAGE_2000)
                            .setCachePath(cachePath)
                            .setType("tianditu")
                            .build();
                    tdtgz2 = new TianDiTuLayerBuilder().setContext(context)
                            .setLayerType(TianDiTuLayerTypes.TIANDITU_IMAGE_ANNOTATION_CHINESE_2000)
                            .setCachePath(cachePath)
                            .setType("tianditu")
                            .build();
                }
                basemap.getBaseLayers().add(tdtgz);
                basemap.getBaseLayers().add(tdtgz2);
            }
            mArcGISMap.setBasemap(basemap);
            progressDialog.dismiss();
        } catch (Exception exception) {
            Log.e("切换地图:", exception.getMessage());
            progressDialog.dismiss();
        }
    }


    /**
     * ===============================国家天地图其他========================
     */

    //国家天地图地形
    protected void loadMapTer(Context context, ArcGISMap mArcGISMap){
        LayerList baseLayers = mArcGISMap.getBasemap().getBaseLayers();
        baseLayers.clear();
        tdt_gz = null;
        tdt_gz2 = null;
        Basemap basemap = new Basemap();
        tdtgz = new TianDiTuLayerBuilder().setContext(context)
                .setLayerType(TianDiTuLayerTypes.TIANDITU_TERRAIN_2000)
                .setCachePath(cachePath)
                .setType("tianditu")
                .build();
        tdtgz2 = new TianDiTuLayerBuilder().setContext(context)
                .setLayerType(TianDiTuLayerTypes.TIANDITU_TERRAIN_ANNOTATION_CHINESE_2000)
                .setCachePath(cachePath)
                .setType("tianditu")
                .build();
                basemap.getBaseLayers().add(tdtgz);
                basemap.getBaseLayers().add(tdtgz2);
            mArcGISMap.setBasemap(basemap);
    }


}
